﻿using AvalonDock.Layout;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace AvalonDockSample.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public ObservableCollection<LayoutDocument> Documents { get; } = new ObservableCollection<LayoutDocument>();

        public ICommand AddTextDocumentCommand { get; }
        public ICommand AddChartDocumentCommand { get; }
        public ICommand AddViewDocumentACommand { get; }
        public ICommand AddViewDocumentBCommand { get; }

        public ICommand CloseDocumentCommand { get; }

        private IRegionManager _regionManager;

        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            AddTextDocumentCommand = new DelegateCommand(AddTextDocument);
            AddChartDocumentCommand = new DelegateCommand(AddChartDocument);
            AddViewDocumentACommand = new DelegateCommand(AddViewDocumentA);
            AddViewDocumentBCommand = new DelegateCommand(AddViewDocumentB);
            CloseDocumentCommand = new DelegateCommand<LayoutDocument>(CloseDocument);
        }

        private void AddViewDocumentA()
        {
            var doc = new DocumentContainerViewModel(_regionManager,"ViewA")
            { 
                Title= "ViewA",
            };
            doc.IsActive = true;
            doc.CanClose = false;
            Documents.Add(doc);
        }

        private void AddViewDocumentB()
        {
            var doc = new DocumentContainerViewModel(_regionManager, "ViewB")
            {
                Title = "ViewB",
            };
            doc.IsActive = true;
            Documents.Add(doc);
        }

        private void AddTextDocument()
        {
            var doc = new TextDocumentViewModel();
            doc.IsActive = true;
            Documents.Add(doc);
        }

        private void AddChartDocument()
        {
            var doc = new ChartDocumentViewModel();
            doc.IsActive = true;
            Documents.Add(doc);
        }

        private void CloseDocument(LayoutDocument doc)
        {
            if (doc != null && Documents.Contains(doc))
            {
                Documents.Remove(doc);
            }
        }

    }
}
